/*
 * rm.scala
 */

package acorn.jash.commands

import acorn.jash.Cmd
import acorn.jash.{Shell,Path}

import java.io.File

class rm extends Cmd {
  override def exec {
    if (args == Nil || args.last.startsWith("-"))
      println("Please specify files to be removed.")

    val f = (Shell.wd changeTo args.last) asFile;
    if (f.isFile && !f.delete) msg(f)
    else if (f isDirectory) {
      var _r = false
      args.init.foreach{arg=>
        if (arg == "-r") {_r = true; rm(f)}
      }
      if (!_r)
        println("Specify option -r to remove directory: " + f)
    }
  }

  private def rm(f:File) {
    if (f isDirectory) {
      f.listFiles.foreach {
        rm(_)
      }
    }
    if (!f.delete) msg(f)
  }

  private def msg(file:File) {
    println("Can't remove: " + file.getAbsolutePath)
  }
}
